package ArrayList.Swing;

import ArrayList.Dao.AddressListDao;
import ArrayList.Dao.AddressListDaoImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddressListSwing extends JFrame implements ActionListener {
    //表现层
    private static final long serialVersionUID = 1L;
    public Object a[][];
    public JButton add_button;
    public JButton update_button;
    public JButton delete_button;
    public JButton query_button;
    public JButton exit_button;
    public JTextField id_field;
    public JTextField name_field;
    public JTextField phone_field;
    public JTextField address_field;
    Box box1, box2;
    JPanel jPanel1, jPanel2;
    Object name[] = { "id", "name", "phone", "address"};
    JTable table;
    JLabel label2, label3, label4, label5, label9;

    public void init_bll() {
        init();
        repaint();
        setResizable(true);
        setBounds(500, 100, 1000, 700);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("个人通讯录系统");
        setVisible(true);
    }

    public void init(){
        JPanel photo = new JPanel() {
            private static final long serialVersionUID = 1L;

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                ImageIcon imageIcon = new ImageIcon("src/main/resources/com/swpu/img/img.png");
                g.drawImage(imageIcon.getImage(), 0, 0, getWidth(), getHeight(), imageIcon.getImageObserver());
            }
        };
        Font font1=new Font("黑体",Font.BOLD,50);
        Font font2=new Font("微软雅黑",1,18);
        label2 = new JLabel("通讯录");
        label2.setFont(font1);
        label3 = new JLabel("姓名");
        label3.setFont(font2);
        label4 = new JLabel("电话");
        label4.setFont(font2);
        label5 = new JLabel("住址");
        label5.setFont(font2);
        label9 = new JLabel("编号(删除或更新时填入)");
        label9.setFont(font2);
        exit_button = new JButton("退出");
        exit_button.addActionListener(this);

        name_field = new JTextField(10);
        phone_field = new JTextField(10);
        address_field = new JTextField(10);
        id_field = new JTextField(10);

        a = new Object[37][4];
        table = new JTable(a, name);
        table.setEnabled(false);
        JScrollPane scrollPane = new JScrollPane(table);
        box1 = Box.createVerticalBox();

        box1.add(Box.createVerticalStrut(30));
        box1.add(label9);

        box1.add(Box.createVerticalStrut(10));
        box1.add(id_field);

        box1.add(Box.createVerticalStrut(10));
        box1.add(label3);

        box1.add(Box.createVerticalStrut(10));
        box1.add(name_field);

        box1.add(Box.createVerticalStrut(10));
        box1.add(label4);

        box1.add(Box.createVerticalStrut(10));
        box1.add(phone_field);

        box1.add(Box.createVerticalStrut(10));
        box1.add(label5);

        box1.add(Box.createVerticalStrut(10));
        box1.add(address_field);
        box1.add(Box.createVerticalStrut(28));
        add_button = new JButton("新增");
        add_button.addActionListener(this);
        box1.add(add_button);
        box1.add(Box.createVerticalStrut(28));

        update_button = new JButton("更新");
        update_button.addActionListener(this);
        box1.add(update_button);
        box1.add(Box.createVerticalStrut(28));

        delete_button = new JButton("删除");
        delete_button.addActionListener(this);
        box1.add(delete_button);
        box1.add(Box.createVerticalStrut(28));

        query_button = new JButton("查询");
        query_button.addActionListener(this);
        box1.add(query_button);
        box1.add(Box.createVerticalStrut(28));

        box1.add(exit_button);
        box1.add(Box.createVerticalStrut(28));

        box2 = Box.createHorizontalBox();
        box2.add(Box.createHorizontalStrut(0));

        box2.add(box1);
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jPanel1.setPreferredSize(new Dimension(200, 640));
        jPanel2.setLayout(new BorderLayout());
        jPanel2.setPreferredSize(new Dimension(700, 640));
        jPanel2.add(scrollPane, BorderLayout.CENTER);
        jPanel1.setLayout(new BorderLayout());
        jPanel1.add(label2, BorderLayout.NORTH);
        jPanel1.add(box2, BorderLayout.CENTER);
        getContentPane().setLayout(new BorderLayout());
        photo.add(jPanel2, BorderLayout.WEST);
        photo.add(jPanel1, BorderLayout.EAST);
        jPanel1.setOpaque(false);
        jPanel2.setOpaque(false);
        getContentPane().add(photo);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        AddressListDao addressListDao = new AddressListDaoImpl();
        //查询
        if (source == query_button) {
            addressListDao.query(a);
            table.updateUI();
        }

        //添加
        else if (source == add_button) {
            addressListDao.add(name_field.getText(),phone_field.getText(),address_field.getText());
            name_field.setText("");
            phone_field.setText("");
            address_field.setText("");
            id_field.setText("");
            table.updateUI();
        }

        // 修改
        else if (source == update_button) {
            addressListDao.update(id_field.getText(),name_field.getText(),phone_field.getText(),address_field.getText());
            name_field.setText("");
            id_field.setText("");
            address_field.setText("");
            phone_field.setText("");
            table.updateUI();
        }

        // 删除
        else if (source == delete_button) {
            addressListDao.delete(id_field.getText());
            id_field.setText("");
            table.updateUI();
        }

        // 退出
        else if (source == exit_button) {
            this.dispose();
        }
    }
}
